package com.imooc.socketexample;

import java.io.*;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Scanner;

/**
 * Created by yijy on 2017/11/7.
 */
public class ClientSocketTest {
    public static void main(String[] args) throws IOException {
        Socket socket = new Socket("localhost",8888);
        try {
            OutputStream os = socket.getOutputStream();
            PrintWriter pw = new PrintWriter(os);
            pw.print("我是客户端A，请求链接服务器！");
            pw.flush();
            socket.shutdownOutput();
            if(!socket.isClosed()){
                InputStream is = socket.getInputStream();
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String message="";
                while ((message=br.readLine())!=null){
                    System.out.println(message);
                }
                socket.shutdownInput();
                os.close();
                br.close();
                is.close();
            }else {
                System.out.println("与服务端断开");
            }
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
